package cn.ddd.linktran.infrastructure.record.mapper;

import cn.ddd.linktran.domain.domainobject.VisitStatisticsDO;
import cn.ddd.linktran.infrastructure.record.po.TransformEventRecord;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.List;

@Mapper
public interface TransformEventRecordMapper extends BaseMapper<TransformEventRecord> {
    @Select({
            "SELECT COUNT(1) AS pvCount,",
            "       IFNULL(COUNT(DISTINCT unique_identity), 0) AS uvCount,",
            "       IFNULL(COUNT(DISTINCT client_ip), 0) AS ipCount,",
            "       SUM(CASE WHEN transform_status = 3 THEN 1 ELSE 0 END) AS effectiveRedirectionCount,",
            "       SUM(CASE WHEN transform_status != 3 THEN 1 ELSE 0 END) AS ineffectiveRedirectionCount,",
            "       DATE_FORMAT(record_time, '%Y-%m-%d') AS statisticsDate,",
            "       compression_code AS compressionCode,",
            "       short_url AS shortUrl,",
            "       long_url AS longUrl ",
            "FROM transform_event_record ",
            "WHERE deleted = 0 ",
            "  AND record_time >= #{start} ",
            "  AND record_time <= #{end} ",
            "GROUP BY compression_code, DATE_FORMAT(record_time, '%Y-%m-%d'), short_url, long_url"
    })
    List<VisitStatisticsDO> selectVisitStatisticsDuration(@Param("start") OffsetDateTime start,
                                                          @Param("end")   OffsetDateTime end);
}
